/*
 *    Copyright 2022 jacky4631 shen ji email:121659050@qq.com
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.dataoke.shoppingguide.app773026.util;

import static android.graphics.Typeface.BOLD;
import static android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
import static com.dataoke.shoppingguide.app773026.bean.Constants.DA_TAO_KE_GET_TAO_WORD_URL;

import android.graphics.Color;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StrikethroughSpan;
import android.text.style.StyleSpan;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.dataoke.shoppingguide.app773026.bean.dataoke.DataokeGetTaoWordResBean;
import com.dataoke.shoppingguide.app773026.bean.dataoke.DataokeGoodsDetailBean;
import com.dataoke.shoppingguide.app773026.bean.dataoke.DataokeTaoWordBean;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
/**
 *
 * @author: shen ji
 * @date: 2022/3/15
 * @email: 121659050@qq.com
 * @wechat: jacky4631
 */
public class DaTaoKeUtil {

    public interface TaoWordCallback{
        void run(DataokeTaoWordBean taoWordBean);
    }

    public static void initTaoWord(String goodsId, TaoWordCallback taoWordCallback) {
        HttpTools.get(String.format(DA_TAO_KE_GET_TAO_WORD_URL,goodsId), new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("TAG", "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("TAG", response.protocol() + " " +response.code() + " " + response.message());
                DataokeGetTaoWordResBean resBean = JSON.parseObject(response.body().string(), DataokeGetTaoWordResBean.class);
                if(resBean.getCode() == 0) {
                    taoWordCallback.run(resBean.getData());
                }
            }
        });
    }

    public static SpannableStringBuilder getEndPrice(DataokeGoodsDetailBean bean) {
        String priceTxt = "￥" + bean.getActualPrice() + " ￥" + bean.getOriginalPrice();
        SpannableStringBuilder builder = new SpannableStringBuilder(priceTxt);

        //ForegroundColorSpan 为文字前景色，BackgroundColorSpan为文字背景色
        ForegroundColorSpan graySpan = new ForegroundColorSpan(Color.GRAY);
        StrikethroughSpan stSpan = new StrikethroughSpan();  //设置删除线样式
        StyleSpan styleSpan = new StyleSpan(BOLD);


        int index = priceTxt.indexOf(" ");

        builder.setSpan(new AbsoluteSizeSpan(15, true), 0, index, SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(styleSpan, 0, index, SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(stSpan, index + 1, priceTxt.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(graySpan, index + 1, priceTxt.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
        return builder;

    }

    public static String getMonthSales(DataokeGoodsDetailBean bean) {
        return "已售" + ParseUtils.getItemSale(bean.getMonthSales()) + "件";
    }
}
